package com.engine.game;

import java.util.ArrayList;


import com.component.game.Component;

public abstract  class MegaGameObject implements GameEntity{

	private ArrayList<Event> events;
	
	private ArrayList<String> listOfChilds;
	
	private GameState state;
	
	public String name;
	
	public boolean isDead;
	
	public MegaGameObject(String name, GameState state){
		this.events = new ArrayList<Event>();
		
		this.listOfChilds = new ArrayList<String>();
		
		this.state = state;
		
		this.isDead = false;
		
		this.name = name;
	}
	
	public void receiveMessage(Event event){
		events.add(event);
	}
	
	public void clearEvents(){
		events.clear();
	}
	
	
	
	public void generate(String message, Component component){
		this.state.pushEvent(Event.createEvent(this.name, message, component));
	}
	
	public void generate(String message, String receiver, Component component){
		
		
		this.state.pushEvent(Event.createEvent(this.name, receiver, message, component));
	}
	
	
	
	@Override
	public abstract void update() throws InstantiationException, IllegalAccessException,
			ClassNotFoundException ;
	

	@Override
	public boolean getIsDead() {
		
		return this.isDead;
	}
	
	public void die(){
		
		this.state.sendToGraveyard(this.name);
		this.isDead = true;
		
		
		
		for(String key : listOfChilds){
			
			this.state.sendToGraveyard(key);
			
			this.generate("die;" + key, "CONFIGURATION", Component.NO);
			
			
		}
		
	}
	
	
	
	public void addChild(String name){
		this.listOfChilds.add(name);
	}
	
	

	@Override
	public void dispose() {
		this.clearEvents();
		this.events = null;
		
		this.listOfChilds = null;
		
	}

	@Override
	public int getStageID() {
		
		return 0;
	}

}
